package web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 演示ServletContext功能三： 域对象 （在不同的资源间实现数据传递-共享数据）
 *      request：同一个请求下的不同资源（转发跳转）
 *      servletcontext：整个应用下的不同资源 （应用级别）
 *
 *      session
 *      pagecontext
 */
@WebServlet("/demo5")
public class ServletDemo5 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();

        req.setAttribute("requestData","requestData...");

        servletContext.setAttribute("applicationData","applicationData...");

        //重定向跳转 (项目路径-/demo2+资源路径-/demo6)
//        resp.sendRedirect(req.getContextPath() + "/demo6");

        //转发跳转
        req.getRequestDispatcher("/demo6").forward(req,resp);
    }
}
